package cn.com.gzheroli.bfw.web.system.controler.core;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;

import org.apache.ibatis.jdbc.SQL;
import org.apache.log4j.Logger;

import cn.com.gzheroli.bfw.pojo.base.DuplicateCheckPage;
import cn.com.gzheroli.bfw.pojo.base.TSIcon;
import cn.com.gzheroli.bfw.pojo.base.TSType;
import cn.com.gzheroli.bfw.pojo.base.TSTypegroup;
import cn.com.gzheroli.bfw.pojo.base.TSVersion;
import cn.com.gzheroli.cache.SpringRedisCache;
import cn.com.gzheroli.core.common.controller.BaseController;
import cn.com.gzheroli.core.common.model.json.AjaxJson;
import cn.com.gzheroli.core.common.model.json.TreeGrid;
import cn.com.gzheroli.core.common.model.json.ValidForm;
import cn.com.gzheroli.core.common.service.CommonService;
import cn.com.gzheroli.core.common.service.SystemService;
import cn.com.gzheroli.core.constant.Globals;
import cn.com.gzheroli.core.manager.Client;
import cn.com.gzheroli.core.manager.ClientSort;
import cn.com.gzheroli.core.util.NameHandler;
import cn.com.gzheroli.core.util.IdWorker;
import cn.com.gzheroli.core.util.LogUtil;
import cn.com.gzheroli.core.util.MyClassLoader;
import cn.com.gzheroli.core.util.StringUtil;
import cn.com.gzheroli.core.util.oConvertUtils;
import cn.com.gzheroli.rabbitmq.service.log.LogService;
import cn.com.gzheroli.service.bfw.TypegroupService;
import cn.com.gzheroli.service.bfw.UserService;
import cn.com.gzheroli.tag.core.DataGrid;
import cn.com.gzheroli.util.TagUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

/**
 * 类型字段处理类
 * 
 * 
 */
@Controller
@RequestMapping("/systemController")
public class SystemController extends BaseController {
	private static final Logger logger = Logger.getLogger(SystemController.class);
	/*@Autowired
	private UserService userService;*/
	@Autowired
	private SystemService systemService;
	private String message;
	@Autowired
	private LogService logService;
	@Autowired
	private CommonService commonService;
	
	@Autowired
	private TypegroupService typegroupService;
	@Autowired
    private  SpringRedisCache cache;
	

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	
	@RequestMapping(params = "druid")
	public ModelAndView druid() {
		return new ModelAndView(new RedirectView("druid/index.html"));
	}
	/**
	 * 类型字典列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "typeGroupTabs")
	public ModelAndView typeGroupTabs(HttpServletRequest request) {
		List<TSTypegroup> typegroupList = commonService.loadAll(TSTypegroup.class);
		request.setAttribute("typegroupList", typegroupList);
		return new ModelAndView("system/type/typeGroupTabs");
	}

	/**
	 * 类型分组列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "typeGroupList")
	public ModelAndView typeGroupList(HttpServletRequest request) {
		return new ModelAndView("system/type/typeGroupList");
	}

	/**
	 * 类型列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "typeList")
	public ModelAndView typeList(HttpServletRequest request) {
		String typegroupid = request.getParameter("typegroupid");
		TSTypegroup typegroup = commonService.getEntity(TSTypegroup.class, typegroupid);
		request.setAttribute("typegroup", typegroup);
		return new ModelAndView("system/type/typeList");
	}

	/**
	 * easyuiAJAX请求数据
	 */

	@RequestMapping(params = "typeGroupGrid")
	public void typeGroupGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		Integer page =request.getParameter("page")==null?1:Integer.parseInt(request.getParameter("page"));
		Integer rows =request.getParameter("rows")==null?20:Integer.parseInt(request.getParameter("rows"));
		Integer start = Integer
				.valueOf((page.intValue() - 1) * rows.intValue()) + 1;
		Integer end = Integer.valueOf(page.intValue() * rows.intValue());
		Map datamap=new HashMap();
		datamap.put("id", request.getParameter("typegroupid"));
		datamap.put("typegroupname", request.getParameter("typegroupname"));
		datamap.put("maxResults", end.intValue());
		datamap.put("firstResult", start.intValue());
		datamap.put("beginRow", (page-1)*rows);//mysql
		datamap.put("pageSize", end.intValue() - start.intValue() + 1);
		List<Map> data=typegroupService.getTypegroupList(datamap);
		Long total=typegroupService.getTypegroupCount(datamap);
		dataGrid.setResults(data);
		dataGrid.setTotal(total.intValue());
		TagUtil.datagrid(response, dataGrid);
	}


	/**
	 * easyuiAJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "typeGrid")
	@ResponseBody
	public List<Map<String,Object>>  typeGrid(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		
		Map datamap=new HashMap();
		datamap.put("typegroupid", request.getParameter("typegroupid"));
		datamap.put("typegroupname", request.getParameter("typegroupname"));
		datamap.put("typegroupcode", request.getParameter("typegroupcode"));
		//List<Map> data=this.typegroupService.getTypegroupAndTypeList(datamap);
		List<Map<String,Object>> data=this.typegroupService.getTypeListPage(datamap);
		
		/*Long total=this.typegroupService.getTypeListPageCount(datamap);
		dataGrid.setResults(data);
		dataGrid.setTotal(total.intValue());
		TagUtil.datagrid(response, dataGrid);*/
		return data;
	}
	
	@RequestMapping(params = "chooseType")
	public void chooseType(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		String typegroupname = request.getParameter("typeGroupName");
		String typegroupcode = request.getParameter("typeGroupCode");
		String typename = request.getParameter("typeName");
		String typecode = request.getParameter("typeCode");
		String typegroupid = request.getParameter("typegroupid");
		/*String hql="from TSType t where 1=1 ";
		if(typegroupname!=null&&!typegroupname.equals(""))
			hql+=" and typeGroupName like '%"+ typegroupname+"%'";
		if(typename!=null&&!typename.equals(""))
			hql+=" and typeName like '%"+typename+"%'";
		if(typecode!=null&&!typecode.equals(""))
			hql+=" and typecode like '%"+typecode+"%'";
		if(typegroupcode!=null&&!typegroupcode.equals(""))
			hql+=" and TSTypegroup.typeGroupCode like '%"+typegroupcode+"%'";
	*/
		Map para=new HashMap();
		para.put("typegroupname", typegroupname);
		para.put("typegroupcode", typegroupcode);
		para.put("typename", typename);
		para.put("typecode", typecode);
		para.put("typegroupid", typegroupid);
		List typeList=typegroupService.getTypeListPage(para);
		int count=typegroupService.getTypeListPageCount(para).intValue();
		//List userList=systemService.findByQueryString(hql);
		dataGrid.setTotal(count);
	    dataGrid.setResults(typeList);
		TagUtil.datagrid(response, dataGrid);
	}
	  // add-start--Author:zhangguoming  Date:20140928 for：数据字典修改
    /**
     * 跳转到类型页面
     * @param request request
     * @return
     */
	@RequestMapping(params = "goTypeGrid")
	public ModelAndView goTypeGrid(HttpServletRequest request) {
		String typegroupid = request.getParameter("typegroupid");
        request.setAttribute("typegroupid", typegroupid);
		return new ModelAndView("system/type/typeListForTypegroup");
	}

	/*@RequestMapping(params = "typeGridTree")
	@ResponseBody
	public List<TreeGrid> typeGridTree(HttpServletRequest request, TreeGrid treegrid) {
		CriteriaQuery cq;
		List<TreeGrid> treeGrids = new ArrayList<TreeGrid>();
		if (treegrid.getId() != null) {
			cq = new CriteriaQuery(TSType.class);
			cq.eq("TSTypegroup.id", treegrid.getId().substring(1));
			cq.add();
			List<TSType> typeList = systemService.getListByCriteriaQuery(cq, false);
			for (TSType obj : typeList) {
				TreeGrid treeNode = new TreeGrid();
				treeNode.setId("T"+obj.getId());
				treeNode.setText(obj.getTypeName());
				treeNode.setCode(obj.getTypeCode());
				treeGrids.add(treeNode);
			}
		} else {
			cq = new CriteriaQuery(TSTypegroup.class);
			List<TSTypegroup> typeGroupList = systemService.getListByCriteriaQuery(cq, false);
			for (TSTypegroup obj : typeGroupList) {
				TreeGrid treeNode = new TreeGrid();
				treeNode.setId("G"+obj.getId());
				treeNode.setText(obj.getTypeGroupName());
				treeNode.setCode(obj.getTypeGroupCode());
				treeNode.setState("closed");
				treeGrids.add(treeNode);
			}
		}

		return treeGrids;
	}*/
	/**
	 * 删除类型分组或者类型（ID以G开头的是分组）
	 * 
	 * @return
	 */
	@RequestMapping(params = "delTypeGridTree")
	@ResponseBody
	public AjaxJson delTypeGridTree(String id, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (id.startsWith("G")) {//分组
			TSTypegroup typegroup = commonService.getEntity(TSTypegroup.class, id.substring(1));
			message = "数据字典分组: " + typegroup.getTypeGroupName() + "被删除 成功";
			commonService.delete(TSTypegroup.class, id.substring(1));
			cache.deleteCache(typegroup.getTypeGroupCode());
		} else {
			TSType type = commonService.getEntity(TSType.class, id.substring(1));
			message = "数据字典类型: " + type.getTypeName() + "被删除 成功";
			commonService.delete(TSType.class, id.substring(1));
			//刷新缓存
			systemService.refleshTypeGroupCach(type.getTypeGroupId());
		}
		logService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));
		
		j.setMsg(message);
		return j;
	}

	/**
	 * 删除类型分组
	 * 
	 * @return
	 */
	@RequestMapping(params = "delTypeGroup")
	@ResponseBody
	public AjaxJson delTypeGroup(TSTypegroup typegroup, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		typegroup = commonService.getEntity(TSTypegroup.class, typegroup.getId());
		message = "类型分组: " + typegroup.getTypeGroupName() + "被删除 成功";
		commonService.delete(TSTypegroup.class, typegroup.getId());
		logService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));
		//刷新缓存
		//systemService.refleshTypeGroupCach(typegroup.getTypeGroupCode());
		cache.deleteCache(typegroup.getTypeGroupCode().toLowerCase());
		j.setMsg(message);
		return j;
	}

	/**
	 * 删除类型
	 * 
	 * @return
	 */
	@RequestMapping(params = "delType")
	@ResponseBody
	public AjaxJson delType(TSType type, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		type = commonService.getEntity(TSType.class, type.getId());
		message = "类型: " + type.getTypeName() + "被删除 成功";
		commonService.delete(TSType.class, type.getId());
		//刷新缓存
		systemService.refleshTypesCach(type);
		logService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));
		j.setMsg(message);
		return j;
	}

	/**
	 * 检查分组代码
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "checkTypeGroup")
	@ResponseBody
	public ValidForm checkTypeGroup(HttpServletRequest request) {
		ValidForm v = new ValidForm();
		String typeGroupCode=oConvertUtils.getString(request.getParameter("param"));
		String code=oConvertUtils.getString(request.getParameter("code"));
		Map para=new HashMap();
		para.put("typeGroupCode", typeGroupCode);
		Long typeGroupSize=typegroupService.checkType(para);
		if(typeGroupSize.intValue()>0&&!code.equals(typeGroupCode))
		{
			v.setInfo("分组已存在");
			v.setStatus("n");
		}
		return v;
	}
	/**
	 * 添加类型分组
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "saveTypeGroup")
	@ResponseBody
	public AjaxJson saveTypeGroup(TSTypegroup typegroup, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		String typeGroupId=typegroup.getId();
		if (StringUtil.isNotEmpty(typegroup.getId())) {
			message = "类型分组: " + typegroup.getTypeGroupName() + "被更新成功";
			//systemService.saveOrUpdate(typegroup);
			typegroupService.updateTypegroup(typegroup);
			logService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));
		} else {
			message = "类型分组: " + typegroup.getTypeGroupName()+ "被添加成功";
			typeGroupId=IdWorker.getFlowIdWorkerInstance().nextIdStr();
			typegroup.setId(typeGroupId);
			typegroupService.saveTypegroup(typegroup);
			logService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));
		}
		//刷新缓存
		systemService.refleshTypeGroupCach(typeGroupId);
		j.setMsg(message);
		return j;
	}
	/**
	 * 检查类型代码
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "checkType")
	@ResponseBody
	public ValidForm checkType(HttpServletRequest request) {
		ValidForm v = new ValidForm();
		String typeCode=oConvertUtils.getString(request.getParameter("param"));
		String code=oConvertUtils.getString(request.getParameter("code"));
		String typeGroupCode=oConvertUtils.getString(request.getParameter("typeGroupCode"));
		
		Map para=new HashMap();
		para.put("typeGroupCode", typeGroupCode);
		para.put("typeCode", typeCode);
		Long typeSize=typegroupService.checkType(para);
		if(typeSize.intValue()>0&&!code.equals(typeCode))
		{
			v.setInfo("类型已存在");
			v.setStatus("n");
		}
		return v;
	}
	/**
	 * 添加类型
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "saveType")
	@ResponseBody
	public AjaxJson saveType(TSType type, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(type.getId())) {
			message = "类型: " + type.getTypeName() + "被更新成功";
			if(type.getTypeId()==null||type.getTypeId().equals(""))
				type.setTypeId(null);
			commonService.update(type);
			logService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));
		} else {
			message = "类型: " + type.getTypeName() + "被添加成功";
			if(type.getTypeId()==null||type.getTypeId().equals(""))
				type.setTypeId(null);
			type.setId(IdWorker.getFlowIdWorkerInstance().nextIdStr());
			commonService.save(type);
			logService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));
		}
		//刷新缓存
		systemService.refleshTypesCach(type);
		j.setMsg(message);
		return j;
	}

	

	/**
	 * 类型分组列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "aouTypeGroup")
	public ModelAndView aouTypeGroup(TSTypegroup typegroup, HttpServletRequest req) {
		if (typegroup.getId() != null) {
			typegroup = commonService.getEntity(TSTypegroup.class, typegroup.getId());
			req.setAttribute("typegroup", typegroup);
		}
		return new ModelAndView("system/type/typegroup");
	}
	/**
	 * 获取数据字典分组信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "getTypeGroupData")
	@ResponseBody
	public AjaxJson getTypeGroupData(TSTypegroup typegroup,HttpServletRequest request) {
		AjaxJson j=new AjaxJson();
		if (typegroup.getId() != null) {
			typegroup = commonService.getEntity(TSTypegroup.class, typegroup.getId());
		}
		j.setObj(typegroup);
		j.setSuccess(true);
		j.setMsg("获取成功");
		return j;
	}
	@RequestMapping(params = "turnPage")
	public ModelAndView turnPage( HttpServletRequest req) {
		Map reqParam=req.getParameterMap();
		String turn=oConvertUtils.getString(req.getParameter("turn"));
		Iterator it = reqParam.keySet().iterator();
		String param="";
		while (it.hasNext()) {
			String key = (String) it.next();
			if(key==null||key.equals("")||key.equals("turn")||key.equals("turnPage")||key.equals("ids"))
				continue;
			String value =(String)req.getParameter(key);
			if(key!=null&&!key.equals("")&&!key.equals("turn"))
				param+="&"+key+"="+value; 
		}
		req.setAttribute("Queryparam", param);
		return new ModelAndView(turn);
	}
	/**
	 * 获取字典分组列表
	* <p>方法名: getTypeByGroupList<／p>
	* <p>描述: <／p>
	* @param request
	* @param response
	* @param dataGrid
	 */
	@RequestMapping(params = "getTypeByGroupList")
	public void getTypeGroupList(HttpServletRequest request,HttpServletResponse response, DataGrid dataGrid){
		String typegroupname=oConvertUtils.getString(request.getParameter("typegroupname"));
		/*String hql="select * from t_s_typegroup where 1=1 ";
		if(!typegroupname.trim().equals(""))
			hql+=" and TYPE_GROUP_NAME='"+typegroupname+"'";*/
		Map para=new HashMap();
		para.put("typegroupname", typegroupname);
		/*SQL sql=new SQL();
		sql.SELECT("*");
		sql.FROM("t_s_typegroup");
		sql.WHERE("TYPE_GROUP_NAME=#{typegroupname}");*/
		String sql="select * from t_s_typegroup where TYPE_GROUP_NAME=#{typegroupname}";
		List<TSTypegroup> typegroupList = commonService.findByQueryString(sql,TSTypegroup.class,para);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 类型列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdateType")
	public ModelAndView addorupdateType(HttpServletRequest req) {
		String typegroupid = req.getParameter("typegroupid");
		String id=req.getParameter("id");
		req.setAttribute("typeGroupId", typegroupid);
        //TSTypegroup typegroup = systemService.findUniqueByProperty(TSTypegroup.class, "id", typegroupid);
        TSTypegroup typegroup =typegroupService.getTypeGroup(typegroupid);
        //List<TSTypegroup> typegroupList= systemService.findByQueryString("from TSTypegroup");
        String typegroupname = typegroup.getTypeGroupName();
        req.setAttribute("typeGroupName", typegroupname);
        req.setAttribute("typeGroupCode", typegroup.getTypeGroupCode());
		if (StringUtil.isNotEmpty(id)) {
			//type = systemService.getEntity(TSType.class, type.getId());
			TSType type =typegroupService.getType(id);
			type.setTypeGroupCode(typegroup.getTypeGroupCode());
			req.setAttribute("type", type);
		}
		
		return new ModelAndView("system/type/type");
	}

	/**
	 * 获取数据字典信息
	 * 
	 * @return
	 */
	@RequestMapping(params = "getTypeData")
	@ResponseBody
	public AjaxJson getTypeData(HttpServletRequest request) {
		AjaxJson j=new AjaxJson();
		String typeGroupId = request.getParameter("typeGroupId");
		String id=request.getParameter("id");
		TSType type=null;
        //TSTypegroup typegroup = systemService.findUniqueByProperty(TSTypegroup.class, "id", typegroupid);
        TSTypegroup typegroup =typegroupService.getTypeGroup(typeGroupId);
        //List<TSTypegroup> typegroupList= systemService.findByQueryString("from TSTypegroup");
        String typegroupname = typegroup.getTypeGroupName();
       // req.setAttribute("typeGroupName", typegroupname);
        //req.setAttribute("typeGroupCode", typegroup.getTypeGroupCode());
		if (StringUtil.isNotEmpty(id)) {
			//type = systemService.getEntity(TSType.class, type.getId());
			type =typegroupService.getType(id);
			type.setTypeGroupCode(typegroup.getTypeGroupCode());
		}
		j.setObj(type);
		j.setSuccess(true);
		j.setMsg("获取成功");
		return j;
	}



	

	/************************************** 版本维护 ************************************/

	/**
	 * 版本维护列表
	 */
	@RequestMapping(params = "versionList")
	public void versionList(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		/*CriteriaQuery cq = new CriteriaQuery(TSVersion.class, dataGrid);
		this.systemService.getDataGridReturn(cq, true);*/
		Map para=new HashMap();
		final String tableName = new NameHandler().getTableName(TSVersion.class.getSimpleName());
		String sql=new SQL(){{
			SELECT("*");
		    FROM(tableName);
			}}.toString();
		dataGrid=this.commonService.getDataGridReturn(dataGrid,TSVersion.class,sql,para,true);
		TagUtil.datagrid(response, dataGrid);
		;
	}

	/**
	 * 删除版本
	 */

	@RequestMapping(params = "delVersion")
	@ResponseBody
	public AjaxJson delVersion(TSVersion version, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		//version = systemService.getEntity(TSVersion.class, version.getId());
		message = "版本：" + version.getVersionName() + "被删除 成功";
		commonService.delete(TSVersion.class, version.getId());
		logService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO,LogUtil.logReqMap(request));

		return j;
	}

	/**
	 * 版本添加跳转
	 * 
	 * @param icon
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "addversion")
	public ModelAndView addversion(HttpServletRequest req) {
		return new ModelAndView("system/version/version");
	}

	/**
	 * 保存版本
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(params = "saveVersion", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson saveVersion(HttpServletRequest request) throws Exception {
		AjaxJson j = new AjaxJson();
		TSVersion version = new TSVersion();
		String versionName = request.getParameter("versionName");
		String versionCode = request.getParameter("versionCode");
		version.setVersionCode(versionCode);
		version.setVersionName(versionName);
		commonService.save(version);
		j.setMsg("版本保存成功");
		return j;
	}

	
	
	/**
	 * 文件添加跳转
	 * 
	 * @param icon
	 * @param req
	 * @return
	 */
	@RequestMapping(params = "addFiles")
	public ModelAndView addFiles(HttpServletRequest req) {
		return new ModelAndView("system/document/files");
	}
	
	
	
	/**
	 * 在线用户列表
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param tSOline
	 */

	@RequestMapping(params = "datagridOnline")
	public void datagridOnline(Client tSOnline,HttpServletRequest request, 
			HttpServletResponse response, DataGrid dataGrid) {
		List<Client> onlines = new ArrayList<Client>();
		//onlines.addAll(ClientManager.getInstance().getAllClient());
		dataGrid.setTotal(onlines.size());
		dataGrid.setResults(getClinetList(onlines,dataGrid));
		TagUtil.datagrid(response, dataGrid);
	}
	/**
	 * 获取当前页面的用户列表
	 * @param onlines
	 * @param dataGrid
	 * @return
	 */
	private List<Client> getClinetList(List<Client> onlines, DataGrid dataGrid) {
		Collections.sort(onlines, new ClientSort());
		List<Client> result = new ArrayList<Client>();
		for(int i = (dataGrid.getPage()-1)*dataGrid.getRows();
				i<onlines.size()&&i<dataGrid.getPage()*dataGrid.getRows();i++){
			result.add(onlines.get(i));
		}
		return result;
	}
	
	@RequestMapping(params = "getDicType", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson getDicType(HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
		String pTypeGroupName = request.getParameter("pTypeGroupName");
		String pTypeGroupCode = request.getParameter("pTypeGroupCode");
		String typename = request.getParameter("typename");
		String typecode = request.getParameter("typecode");
		String ptypecode = request.getParameter("ptypecode");
		Map para=new HashMap();
		para.put("pTypeGroupName", pTypeGroupName);
		para.put("typename", typename);
		para.put("typecode", typecode);
		para.put("pTypeGroupCode", pTypeGroupCode);
		para.put("ptypecode", ptypecode);
		String sql="select t.* from t_s_type t inner join  t_s_typegroup g on t.TYPE_GROUP_ID=g.ID left join on t_s_type pt on t.TYPEP_ID=pt.ID";
		sql=sql+" and g.TYPE_GROUP_NAME like #{pTypeGroupName}";
		sql=sql+" and t.TYPE_NAME like #{typename}";
		sql=sql+" and t.TYPE_CODE like #{typecode}";
		sql=sql+" and g.TYPE_GROUP_CODE=#{pTypeGroupCode}";
		sql=sql+" and pt.TYPE_CODE=#{ptypecode}";
		List<TSType> userList=commonService.findByQueryString(sql,TSType.class,para);
		List typeList=new ArrayList();
		if(userList!=null&&!userList.isEmpty())
		for(TSType type:userList){
			JSONObject obj=new JSONObject();
			obj.put("typecode", type.getTypeCode());
			obj.put("typename", type.getTypeName());
			typeList.add(obj);
		}
		AjaxJson j=new AjaxJson();
		j.setSuccess(true);
		j.setObj(typeList);
		Map attributes=new HashMap();
		//attributes.put("typeList", userList);
		j.setAttributes(attributes);
		return j;
	}
	
	/**
	 * 校验数据是否在系统中是否存在
	 * @return
	 */
	@RequestMapping(params = "doDuplicateCheck")
	@ResponseBody
	public AjaxJson doDuplicateCheck(DuplicateCheckPage duplicateCheckPage, HttpServletRequest request) {

		AjaxJson j = new AjaxJson();
		Long num = null;
		
		num=systemService.doDuplicateCheck(duplicateCheckPage);
		
		if(num==null||num==0){
			//该值可用
			j.setSuccess(true);
			j.setMsg("该值可用！");
		}else{
			//该值不可用
			j.setSuccess(false);
			j.setMsg("该值不可用，系统中已存在！");
		}
		return j;
	}
	@RequestMapping(params = "getTypeComboTree")
	@ResponseBody
	public List<Map<String,Object>>  getTypeComboTree(HttpServletRequest request) throws Exception {
		String parentid=request.getParameter("parentid")==null?"":request.getParameter("parentid");
		String typeGroupId=request.getParameter("typeGroupId")==null?"":request.getParameter("typeGroupId");
		Map para=new HashMap();
		if(parentid==null||parentid.equals(""))
			para.put("pid",null);
		else {
			para.put("pid", parentid);
		}
		para.put("typeGroupId", typeGroupId);
		return typegroupService.getTypeComboTree(para);
	}
	@RequestMapping(params = "getTypeTree")
	@ResponseBody
	public List<Map<String,Object>>  getTypeTree(HttpServletRequest request) throws Exception {
		String parentid=request.getParameter("id")==null?"":request.getParameter("id");
		String typeGroupId=request.getParameter("typeGroupId")==null?"":request.getParameter("typeGroupId");
		Map para=new HashMap();
		if(parentid==null||parentid.equals(""))
			para.put("pid",null);
		else {
			para.put("pid", parentid);
		}
		para.put("typeGroupId", typeGroupId);
		return typegroupService.getTypeComboTree(para);
	}
}
